package com.fosun.spr.plugin.learn.api.v2Tbitem.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fosun.spr.plugin.learn.api.v2Tbitem.dao.V2TbitemDao;
import com.fosun.spr.plugin.learn.api.v2Tbitem.service.V2TbitemService;
import com.fosun.spr.plugin.learn.api.v2Tbitem.service.model.CreateOrUpdateSpModel;
import com.fosun.spr.plugin.learn.api.v2Tbitem.service.model.CreateOrUpdateSrModel;
import com.fosun.spr.plugin.learn.api.v2Tbitem.service.model.DeleteSpModel;
import com.fosun.spr.plugin.learn.api.v2Tbitem.service.model.DeleteSrModel;
import com.fosun.spr.plugin.learn.api.v2Tbitem.service.model.GetSpModel;
import com.fosun.spr.plugin.learn.api.v2Tbitem.service.model.GetSrModel;
import com.fosun.spr.plugin.learn.api.v2Tbitem.service.model.ListSpModel;
import com.fosun.spr.plugin.learn.api.v2Tbitem.service.model.ListSrModel;
import com.fosun.spr.plugin.learn.helper.mybatis.dao.V2TbitemMapper;
import com.fosun.spr.plugin.learn.helper.mybatis.model.V2Tbitem;
import com.fosun.spr.plugin.learn.helper.mybatis.model.V2Tbitem;
import com.fosun.spr.plugin.learn.helper.mybatis.model.V2TbitemExample;
import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.exception.ServiceException;
import com.fosun.spr.core.utils.ExtAsserts;


@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
@Service
public class V2TbitemServiceImpl extends CoBaseService implements
		V2TbitemService {
	@Autowired 
	private V2TbitemDao v2TbitemDao;
	@Autowired
	private V2TbitemMapper v2TbitemMapper;

	@Override
	public ListSrModel list(ListSpModel spModel) {
		ListSrModel srModel = new ListSrModel();
		srModel.setDatas(v2TbitemDao.list(spModel));
		srModel.setTotalCount(v2TbitemDao.listCount(spModel));
		return srModel;
	}

	@Override
	public GetSrModel get(GetSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		GetSrModel srModel = new GetSrModel();
		V2Tbitem db = v2TbitemMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db,"淘宝商品管理-v2_tbitem");
		cglibCopyProperties(db, srModel, true);	
		return srModel;
	}
	
	@Override
	public DeleteSrModel delete(DeleteSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		DeleteSrModel srModel = new DeleteSrModel();
		V2Tbitem db = v2TbitemMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db);
		db.setIsDeleted(IS_DELETED_Y);
		db.setRecUpdateTime(new Date());
		v2TbitemMapper.updateByPrimaryKey(db);
		return srModel;
	}
	
	@Override
	public CreateOrUpdateSrModel createOrUpdate(CreateOrUpdateSpModel spModel) {
		ExtAsserts.notNull(spModel.getTitle(),"商品名称");
		ExtAsserts.notNull(spModel.getFromUrl(),"商品原链接");
		ExtAsserts.notNull(spModel.getTbUrl(),"商品联盟链接");
		ExtAsserts.notNull(spModel.getPrices(),"价格");
		ExtAsserts.notNull(spModel.getFileImage(),"商品图片");
		
		CreateOrUpdateSrModel srModel = new CreateOrUpdateSrModel();
		//唯一性校验
		V2TbitemExample eTitle = new V2TbitemExample();
		V2TbitemExample.Criteria cTitle = eTitle.createCriteria();
		cTitle.andTitleEqualTo(spModel.getTitle());
		cTitle.andIsDeletedEqualTo(IS_DELETED_N);
		List<V2Tbitem> rowsTitle = v2TbitemMapper.selectByExample(eTitle);		
		
		//if(StringUtils.isBlank(spModel.getId())){
		if(spModel.getId()==null){
			if(rowsTitle.size()>0) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getTitle()+" 已经存在");
			}		
			
			V2Tbitem db = new V2Tbitem();
			cglibCopyProperties(spModel, db, true);
				db.setTbUrlTime(new Date());
			db.setIsDeleted(IS_DELETED_N);
			db.setRecCreateTime(new Date());
			//db.setId(getUuid());
			v2TbitemMapper.insert(db);
			srModel.setId(db.getId());
		}else{
			if(rowsTitle.size()>1) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getTitle()+" 已经存在");
			}			
			if(rowsTitle.size()==1 && !rowsTitle.get(0).getTitle().equals(spModel.getTitle())) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getTitle()+" 已经存在");
			}	
			
			V2Tbitem db = v2TbitemMapper.selectByPrimaryKey(spModel.getId());
			db.setTitle(spModel.getTitle());
			db.setFromUrl(spModel.getFromUrl());
			db.setTbUrl(spModel.getTbUrl());
			db.setPrices(spModel.getPrices());
			db.setPricesUp(spModel.getPricesUp());
			db.setFileImage(spModel.getFileImage());
			db.setInitFavorites(spModel.getInitFavorites());
			db.setNo(spModel.getNo());
			db.setTbshopId(spModel.getTbshopId());
			v2TbitemMapper.updateByPrimaryKey(db);
		}
		return srModel;
	}	
}
